package com.herui.iot.ui.evaluation.execute;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.TextView;

import com.herui.device_lib.presenter.DevicePresenterInter;
import com.herui.sdyu_lib.base.BaseActivity;
import com.herui.sport_care.R;
import com.herui.sport_care.ui.dialog.BpManagerDialog;
import com.herui.sport_care.ui.dialog.SpoManagerDialog;
import com.protontek.healthy.sdkcore.sphygmomanometer.api.BPDataBean;

import androidx.annotation.Nullable;
import butterknife.BindView;

/**
 * Created by amanda.shan on 2022/12/13.
 */
public class RecoverDetectView{

    View view_recover_detect;
    View view_bp_detect1;
    View view_bp_detect2;
    View view_bp_detect3;
    private TextView[] bpTitleTextViews = new TextView[3];
    private TextView[] bpValueTextViews = new TextView[3];
    private TextView[] bpConnetTextViews = new TextView[3];
    View view_spo_detect;
    TextView tv_spo;
    TextView tv_connect_spo;

    private BaseActivity baseActivity;

    public RecoverDetectView(View view_recover_detect) {
        this.view_recover_detect = view_recover_detect;

    }

    public void initUI(BaseActivity baseActivity, DevicePresenterInter devicePresenter,
                        BaseExecutePresenter baseExecutePresenter){
        this.baseActivity = baseActivity;

        view_bp_detect1 = view_recover_detect.findViewById(R.id.view_bp_detect1);
        view_bp_detect2 = view_recover_detect.findViewById(R.id.view_bp_detect2);
        view_bp_detect3 = view_recover_detect.findViewById(R.id.view_bp_detect3);
        view_spo_detect = view_recover_detect.findViewById(R.id.view_spo_detect);

        bpTitleTextViews[0] = view_bp_detect1.findViewById(R.id.tv_title);
        bpTitleTextViews[0].setText("第一次血压");
        bpTitleTextViews[1] = view_bp_detect2.findViewById(R.id.tv_title);
        bpTitleTextViews[1].setText("第二次血压");
        bpTitleTextViews[2] = view_bp_detect3.findViewById(R.id.tv_title);
        bpTitleTextViews[2].setText("第三次血压");

        bpValueTextViews[0] = view_bp_detect1.findViewById(R.id.tv_bp);
        bpValueTextViews[1] = view_bp_detect2.findViewById(R.id.tv_bp);
        bpValueTextViews[2] = view_bp_detect3.findViewById(R.id.tv_bp);

        bpConnetTextViews[0] = view_bp_detect1.findViewById(R.id.tv_connect_bp);
        bpConnetTextViews[1] = view_bp_detect2.findViewById(R.id.tv_connect_bp);
        bpConnetTextViews[2] = view_bp_detect3.findViewById(R.id.tv_connect_bp);
        for (int i = 0; i < bpConnetTextViews.length; i++) {
            int finalI = i;
            bpConnetTextViews[i].setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    new BpManagerDialog(baseActivity, devicePresenter,
                            new BpManagerDialog.DialogLis() {
                                @Override
                                public void onOk(BPDataBean bpDataBean) {
                                    baseExecutePresenter.saveBp(finalI, bpDataBean);
                                    bpValueTextViews[finalI].setVisibility(View.VISIBLE);
                                    bpConnetTextViews[finalI].setVisibility(View.INVISIBLE);
                                    bpValueTextViews[finalI].setText(bpDataBean.getSystolicBp() + "/" + bpDataBean.getDiastolicBp());
                                }
                            });
                }
            });
        }

        tv_spo = view_spo_detect.findViewById(R.id.tv_spo);
        tv_connect_spo = view_spo_detect.findViewById(R.id.tv_connect_spo);
        tv_connect_spo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new SpoManagerDialog(baseActivity, devicePresenter, new SpoManagerDialog.DialogLis() {
                    @Override
                    public void onOk() {

                    }
                });
            }
        });
    }

    public void onConnectSpo(boolean flag) {
        baseActivity.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (flag) {
                    tv_connect_spo.setVisibility(View.GONE);
                    tv_spo.setVisibility(View.VISIBLE);
                } else {
                    tv_connect_spo.setVisibility(View.VISIBLE);
                    tv_spo.setVisibility(View.GONE);
                }
            }
        });
    }

    public void onReceiveSpO2(int spo) {
        baseActivity.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                tv_spo.setText(spo + "%");
            }
        });
    }
}
